﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WPLDB.Infrastucture.UI.ViewModelBase;
using WPLDB.DA.Interfaces.UI;
using System.ComponentModel.Composition;
using Microsoft.Practices.Prism.Events;
using WPLDB.DA.Models;
using WPLDB.Infrastucture.UI.Models;
using WPLDB.Infrastucture.UI.Events;
using WPLDB.DA.Interfaces.UI.Base;
using WPLDB.DA.Models.UI;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Data.ConnectionUI;
using System.Windows.Forms;
using System.Data.SqlClient;
using WPLDB.Utility.Extensions;

namespace WPLDB.Package.UI.ViewModel
{

    /// <summary>
    /// This View is used to select the database object from user.
    /// New Connection command call the ConnectionUI.Dialog for collect the connection information.
    /// </summary>
    [Export(typeof(IChooseDatabaseViewModel))]
    [Export(typeof(IChooseViewModel))]
    public class ChooseDatabaseViewModel : ViewModelBase, IChooseDatabaseViewModel
    {

        #region "Variables"
        private DatabaseModel _model;

        [Import]
        private IChooserModel _chooserModel = null; 
        #endregion

        #region "ctor"

        [ImportingConstructor]
        public ChooseDatabaseViewModel(IChooseDatabaseView view, IChooserModel model)
        {
            OnChooserLoad();
            this.View = view;
            this.View.SetViewModel(this);
        }


        #endregion

        #region "Interface Implementation"

        public bool IsValid
        {
            get
            {
                return null != _chooserModel 
                    && !string.IsNullOrWhiteSpace(_chooserModel.ConnectionString);
            }
        }

        public void OnChooserLoad()
        {
            base.Index = 1;

            #region _model Initialization

            _model = new DatabaseModel
            {
                Name = "Choose Your Data Connection",
                Question = "Which data connection should your application use to connect to the database?",
                Description = "Connection string:",
                ConnectionString = "[ConnectionString]"
            };

            #endregion

            base.RaiseEvent(Name);
        }

        public DA.Interfaces.UI.Base.IBaseView View
        {
            get;
            set;
        }

        #endregion

        #region "Properties"

        public string Question
        {
            get
            {
                return _model.Question;
            }
        }

        public string Name
        {
            get
            {
                return _model.Name;
            }
        }

        public string Description
        {
            get
            {
                return _model.Description;
            }
        }

        public string ConnectionString
        {
            get
            {
                return _model.ConnectionString;
            }
            set
            {
                if (value != _model.ConnectionString)
                {
                    _model.ConnectionString = value;
                    _chooserModel.ConnectionString = value;
                    base.RaiseEvent(Name);
                    base.OnPropertyChanged(() => ConnectionString);

                }
            }
        }

        private string _displayConnectionString;

        public string DisplayConnectionString
        {
            get { return _displayConnectionString; }
            set
            {
                if (value != _displayConnectionString)
                {
                    _displayConnectionString = value;
                    base.OnPropertyChanged(() => DisplayConnectionString);

                }
            }
        }

        private string _selectedDatabase;

        public string SelectedDatabase
        {
            get { return _selectedDatabase ?? (_selectedDatabase = "[Select Database]"); }
            set
            {
                if (value != null)
                {
                    _selectedDatabase = value;
                    base.OnPropertyChanged(() => SelectedDatabase);
                }

            }
        }
        #endregion

        #region "Command"

        private DelegateCommand _newConnectionCommand;

        public DelegateCommand NewConnectionCommand
        {
            get
            {
                return _newConnectionCommand ?? (_newConnectionCommand = new DelegateCommand(() => this.NewConnection(), () => this.CanNewConnection));
            }
        }

        #endregion

        #region "Methods"

        /// <summary>
        /// This method call the ConnectionUI.Dialog window and collection the connection information.
        /// </summary>
        private void NewConnection()
        {
            DataConnectionDialog dcd = new DataConnectionDialog();
            DataConnectionConfiguration dcs = new DataConnectionConfiguration(null);
            dcs.LoadConfiguration(dcd);


            if (DataConnectionDialog.Show(dcd) == DialogResult.OK)
            {
                ConnectionString = dcd.ConnectionString;
                DisplayConnectionString = dcd.DisplayConnectionString;
                SelectedDatabase = string.Format("{0}.{1}.dbo", dcd.ConnectionString.DataSource(), dcd.ConnectionString.InitialCatalog());

            }

            dcs.SaveConfiguration(dcd);
        }     

        #endregion

        #region "Predicator"
        private bool CanNewConnection { get { return true; } } 
        #endregion


    }
}
